<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Datei: plugin.class.php
 *
 * Class to edit all plugin-configs
 *
 * @author Norbert Gebert
 * @copyright Norbert Gebert 02.01.2015
 * @license GPL3
 *
 * @version 0.6b
 * @package Admin
 * @subpackage global
 */
class xplugin
{

	private static $_path = 'xplugin';
	private static $_buffer = '';
	private static $_xPluginClass;

	/**
	 * Initialize
	 */
	function __construct()
	{
		
	}

	/**
	 * Returns an array with all registered plugins and settings
	 * @return array Associative array with plugin-data
	 */
	public static function getPluginNames()
	{
		$result = db::getInstance()->get('plugin');
		$names = array();

		foreach ($result as $entry)
		{
			$names[] = array('name' => $entry['plugin_name'],
			 'active' => $entry['plugin_active']);
		}
		return $names;
	}

	/**
	 * Search new/deleted xplugins and save them in database
	 */
	public static function getNewPlugin()
	{
		// Find all installed Plugins
		$pluginConfig = array();

		if ($handle = opendir(self::$_path))
		{
			while (false !== ($file = readdir($handle)))
			{
				if (is_dir(self::$_path . '/' . $file) and $file <> '..')
				{
					$xFile = self::$_path . '/' . $file . '/' . 'config.php';
					if (is_file($xFile))
						$pluginConfig[] = array('file' => $xFile, 'dir' => $file);
				}
			}
			closedir($handle);
		}

		foreach ($pluginConfig as $entry)
		{ // Compare list with db-entrys
			include_once $entry['file'];

			db::getInstance()->where('plugin_dir', $entry['dir']);
			db::getInstance()->where('plugin_name', $xPluginConfig[0]['plugin_name']);
			$result = db::getInstance()->getOne('plugin');

			// if not found create new entry
			if (db::getInstance()->count <= 0)
			{
				$data = array(
				 'plugin_dir' => $entry['dir'],
				 'plugin_name' => $xPluginConfig[0]['plugin_name'],
				 'plugin_code' => $xPluginConfig[0]['plugin_code'],
				 'plugin_config' => $xPluginConfig[0]['plugin_config'],
				 'plugin_typ' => $xPluginConfig[0]['plugin_typ'],
				 'plugin_help' => $xPluginConfig[0]['plugin_help']
				);
				db::getInstance()->insert('plugin', $data);
			}

			unset($xPluginConfig);
		}
		unset($result);
	}

	/**
	 * Returned all config values for plugin from database
	 * @return array assoziative for template output
	 */
	public static function getValues($idx)
	{
		db::getInstance()->where('plugin_id', $idx);
		$result = db::getInstance()->getOne('plugin');
		self::$_xPluginClass = $result['plugin_dir'];
		self::_startClass();

		return self::$_xPluginClass->getConfigHtml($idx);
	}

	/**
	 * Save all values from plugin editorpage in database
	 * @return boolean True if successful saved, otherwise false
	 */
	public static function saveValues($idx)
	{

		db::getInstance()->where('plugin_id', $idx);
		$result = db::getInstance()->getOne('plugin');
		self::$_buffer = $result['plugin_config'];
		self::$_xPluginClass = $result['plugin_dir'];
		self::_startClass();

		if ($result['plugin_typ'] === 'H')
		{
			if (isset($_POST['activex']))
			{
				$data = array('plugin_active' => 1);
			}
			else
			{
				$data = array('plugin_active' => 0);
			}

			db::getInstance()->where('plugin_id', $idx);
			if (db::getInstance()->update('plugin', $data))
				message::setNoteRow(_('Active status changed'));
		}
		else
		{
			self::$_xPluginClass->saveConfigdata($idx);
		}
	}

	/**
	 * Test if class loaded else load class
	 * @return boolean True if successful open/opened, otherwise false
	 */
	private static function _startClass()
	{
		$pagefile = 'xplugin/' . self::$_xPluginClass . '/' . self::$_xPluginClass . '.xplugin.php';
		if (is_file($pagefile))
			include_once $pagefile;

		self::$_xPluginClass = new self::$_xPluginClass;
		//include_once '/xplugin/' . $classname . '/' . $classname . '.xplugin.php';
	}

}

?>